$(function () {
    init();
})

function init() {
    FastClick.attach(document.body);
    myTab("#tabBt", "#tabPage");
    getList1();getList2()
}

function formHide() {
    $(".weui-mask").remove();
    form.css("transform", "scale(0.1)").css("opacity", "0.1");
    setTimeout(function () {
        form.remove();
    }, 300)
    close.remove();
    submit.remove();
}

function getList1() {
    equipmemtService.queryMyJob({
        rstate: "'209001','209002'",
        estate: "'201402','201501'",
        queryDetail: "1"
    }, function (data) {
        if (data.success) {
            $("#page1").html("");
            $(data.datas).each(function (index, item) {
                var row = $('<div class="row">\n' +
                    '                   <div class="type">\n' +
                    '                       <span class="fa fa-tools"> ' + item.questionTypeName + '</span>\n' +
                    '                   </div>\n' +
                    '                   <div class="flex">\n' +
                    '                       <div class="center">\n' +
                    '                           <div class="title">' + item.name + '</div>\n' +
                    '                           <div class="subTitle">' + item.orgName + " " + item.usePosition + '</div>\n' +
                    '                           <div class="subTitle"> ' + item.questionDescripe + '</div>\n' +
                    '                       </div>\n' +
                    '                       <div class="right"  style="width:0px;">\n' +
                    //    '                           <div class="yy fa fa-clock"><span>预约</span></div>\n' +
                    '                       </div>\n' +
                    '                   </div>\n' +
                    '                   <div class="flex">\n' +
                    '                       <div class="center">\n' +
                    '                           <div class="flex" name="applyImgs">\n' +
                    '                           </div>\n' +
                    '                           <div class="timeLine" id="submitRecord">\n' +
                    '                           </div>\n' +
                    '                           <div class="tips" id="tips">\n' +
                    '                           </div>\n' +
                    '                       </div>\n' +
                    '                       <div class="right" style="width:120px;" id="menus">\n' +
                    '                           <a class="link fa fa-phone-alt" name="applayTel"  href="tel:' + item.telephone + '"> 联系报修人</a>\n' +
                    '                       </div>\n' +
                    '                   </div>\n' +
                    '               </div>');

                $("#page1").append(row);
                if (item.state == "201402") {
                    var result = $(" <a class=\"link fa fa-tools\"  name='repairResult'> 接单受理</a>");
                    row.find("#menus").append(result);
                    result.on("click", function () {
                        $.modal({
                            title: "确认受理",
                            text: "是否受理此业务？",
                            buttons: [
                                {
                                    text: "接受派单",
                                    onClick: function () {
                                        equipmemtService.updateRepairWorkState({
                                            id: item.sdId,
                                            state: "209002",
                                            eType:item.eType,
                                            repairApplyId: item.id,
                                            repairApplyState: "201501"
                                        }, function (data) {
                                            if (data.success) {
                                                getList1();
                                                getList2();
                                            } else {
                                                $.alert(data.errorMsg);
                                            }
                                        });
                                    }
                                },
                                {
                                    text: "拒绝接单", className: "bt_warn",
                                    onClick: function () {
                                        $.confirm("确认拒绝？", "确认", function () {
                                            equipmemtService.updateRepairWorkState({
                                                id: item.sdId,
                                                eType:item.eType,
                                                state: "209003",
                                                repairApplyId: item.id,
                                                repairApplyState: "201502"
                                            }, function (data) {
                                                if (data.success) {
                                                    getList1();
                                                    getList2();
                                                } else {
                                                    $.alert(data.errorMsg);
                                                }
                                            });
                                        })
                                    }
                                },
                                {text: "取消操作", className: "default"},
                            ]
                        });
                    })
                } else {
                    var result = $(" <a class=\"link fa fa-tools\"  name='repairResult'> 完成修理</a>");
                    row.find("#menus").append(result);
                    result.on("click", function () {
                        window.location.href = "eresult/" + item.flowCode;
                    })
                }
                $(item.supplement).each(function (si, sd) {
                    if (sd.dataType == "111001") {
                        var img = $('<div class="imgBox" style="background-image: url(' + sd.fileAddress + ')"  title="' + sd.createTime + "：" + sd.fileDescribe + '">' + sd.fileDescribe + '</div>');
                        var boxs = row.find("[name='applyImgs']");
                        var box = boxs.eq(boxs.length - 1);
                        if (box.children().length == 3) {
                            box.after($("<div class=\"flex\" name=\"applyImgs\"></div>"));
                            box = box.next();
                        }
                        box.append(img);
                        row.find(".imgBox").each(function (i, dom) {
                            var o = $(dom);
                            o.css("height", o.width() + "px");
                        });
                        img.on("click", function (e) {
                            var imgBox = $(e.target);
                            $(".weui-mask").remove();
                            $("body").append('<div class="weui-mask weui-mask--visible"></div>');
                            var imgShow = $("<div class='imgShow'>" + imgBox.attr("title") + "</div>");
                            var close = $("<span class='close fa fa-times'> </span>");
                            $("body").append(imgShow);
                            imgShow.css("background-image", imgBox.css("background-image"));
                            close.on("click", function () {
                                $(".weui-mask").remove();
                                imgShow.remove();
                                close.remove();
                                $("body").off("touchmove");
                                $("body").off("touchend");
                            })
                            $("body").append(imgShow);
                            $("body").append(close);
                            var box = imgShow[0];
                            var boxGesture = setGesture(box);  //得到一个对象
                            boxGesture.gesturemove = function (e) {  //双指移动
                                box.style.transform = "scale(" + e.scale + ")";//改变目标元素的大小
                            };
                        })

                    } else if (sd.dataType == "111002") {
                        var li = $('<li class="fa fa-exclamation-circle"> ' + sd.createTime.substr(5, 11) + " " + sd.fileDescribe + '</li>');
                        row.find("#tips").append(li);
                    }
                })
            })

        } else {
            $.alert(data.errorMsg);
        }
    })
}


function getList2() {
    equipmemtService.queryMyJob({
        rstate: "'209001','209002'",
        estate: "'201601','201701','201702','201801'",
        queryDetail: "1"
    }, function (data) {
        if (data.success) {
            $("#page3").html("");
            $(data.datas).each(function (index, item) {
                var row = $('<div class="row">\n' +
                    '                   <div class="type">\n' +
                    '                       <span class="fa fa-tools"> ' + item.questionTypeName + '</span>\n' +
                    '                   </div>\n' +
                    '                   <div class="flex">\n' +
                    '                       <div class="center">\n' +
                    '                           <div class="title">' + item.name + '</div>\n' +
                    '                           <div class="subTitle">' + item.orgName + " " + item.usePosition + '</div>\n' +
                    '                           <div class="subTitle"> ' + item.questionDescripe + '</div>\n' +
                    '                       </div>\n' +
                    '                       <div class="right"  style="width:0px;">\n' +
                    //    '                           <div class="yy fa fa-clock"><span>预约</span></div>\n' +
                    '                       </div>\n' +
                    '                   </div>\n' +
                    '                   <div class="flex">\n' +
                    '                       <div class="center">\n' +
                    '                           <div class="flex" name="applyImgs">\n' +
                    '                           </div>\n' +
                    '                           <div class="timeLine" id="submitRecord">\n' +
                    '                           </div>\n' +
                    '                           <div class="tips" id="tips">\n' +
                    '                           </div>\n' +
                    '                       </div>\n' +
                    '                       <div class="right" style="width:120px;" id="menus">\n' +
                    '                           <a class="link fa fa-phone-alt" name="applayTel"  href="tel:' + item.telephone + '"> 联系报修人</a>\n' +
                    '                       </div>\n' +
                    '                   </div>\n' +
                    '               </div>');

                $("#page3").append(row);

                $(item.supplement).each(function (si, sd) {
                    if (sd.dataType == "111001") {
                        var img = $('<div class="imgBox" style="background-image: url(' + sd.fileAddress + ')"  title="' + sd.createTime + "：" + sd.fileDescribe + '">' + sd.fileDescribe + '</div>');
                        var boxs = row.find("[name='applyImgs']");
                        var box = boxs.eq(boxs.length - 1);
                        if (box.children().length == 3) {
                            box.after($("<div class=\"flex\" name=\"applyImgs\"></div>"));
                            box = box.next();
                        }
                        box.append(img);
                        row.find(".imgBox").each(function (i, dom) {
                            var o = $(dom);
                            o.css("height", o.width() + "px");
                        });
                        img.on("click", function (e) {
                            var imgBox = $(e.target);
                            $(".weui-mask").remove();
                            $("body").append('<div class="weui-mask weui-mask--visible"></div>');
                            var imgShow = $("<div class='imgShow'>" + imgBox.attr("title") + "</div>");
                            var close = $("<span class='close fa fa-times'> </span>");
                            $("body").append(imgShow);
                            imgShow.css("background-image", imgBox.css("background-image"));
                            close.on("click", function () {
                                $(".weui-mask").remove();
                                imgShow.remove();
                                close.remove();
                                $("body").off("touchmove");
                                $("body").off("touchend");
                            })
                            $("body").append(imgShow);
                            $("body").append(close);
                            var box = imgShow[0];
                            var boxGesture = setGesture(box);  //得到一个对象
                            boxGesture.gesturemove = function (e) {  //双指移动
                                box.style.transform = "scale(" + e.scale + ")";//改变目标元素的大小
                            };
                        })

                    } else if (sd.dataType == "111002") {
                        var li = $('<li class="fa fa-exclamation-circle"> ' + sd.createTime.substr(5, 11) + " " + sd.fileDescribe + '</li>');
                        row.find("#tips").append(li);
                    }
                })
            })

        } else {
            $.alert(data.errorMsg);
        }
    })
}